package a.storeGenerator.ruToken;

import com.asteros.crypto.config.AppConfig;
import com.asteros.crypto.test.Test;
import com.asteros.crypto.utils.services.CryptoServiceConfig;
import ru.signalcom.crypto.cms.Signer;
import ru.signalcom.io.FileFactory;
import ru.signalcom.io.FileInputStreamFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.CertificateFactory;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;

public class Test3_PFXExpand {
	private static ArrayList<Signer> signers = new ArrayList<Signer>();
	private static ArrayList<X509Certificate> certs = new ArrayList<X509Certificate>();
	private static HashSet<TrustAnchor> trust = new HashSet<TrustAnchor>();
	
	public static void main(String[] args) {
		try {
			String password = "password";
			String path = "rutoken 0:/0001/0001";
			String seed = "rutoken 0:";
			
			File file = FileFactory.getInstance(path );
			InputStream in = FileInputStreamFactory.getInstance(file);

			KeyStore store = KeyStore.getInstance("PKCS12", "SC");
			store.load(in, password.toCharArray());
			
			extractStore(store, password, path , seed);
			testSign();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	private static void extractStore(KeyStore keyStore, String password, String path, String seed) throws  Exception{
	
		SecureRandom random = SecureRandom.getInstance("GOST28147PRNG", "SC");
		//random.setSeed(seed.getBytes());
		random.setSeed("C:\\Projects\\RKK\\testdata\\alice".getBytes());
	
		Enumeration<String> aliases = keyStore.aliases();
		while (aliases.hasMoreElements()) {
			String alias = aliases.nextElement();
			if (keyStore.isCertificateEntry(alias)) {
				
				X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
				System.out.println("trusted: " + cert.getSubjectX500Principal());
				trust.add(new TrustAnchor(cert, null));
				
			} else if (keyStore.isKeyEntry(alias)) {
				
				PrivateKey priv = (PrivateKey) keyStore.getKey(alias, password.toCharArray());
				X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
				certs.add(cert);
				
				//�������� ����������				
				Signer signer = new Signer(priv, cert, random);
				signer.setAddSignerCertificate(true);
				signer.setAddSigningCertificate(true);
				signer.setAddSigningTime(true);
	
				signers.add(signer);
			}
		}

        trustTsa();
        trustMain();
	}

    private static void trustTsa() {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509", "SC");
            InputStream in = new FileInputStream(new File("C:\\Projects\\RKK\\testdata\\appserver1\\ext\\tsa.cer"));
            X509Certificate certificate = (X509Certificate) cf.generateCertificate(in);
            in.close();
            trust.add(new TrustAnchor(certificate, null));
        } catch (Exception e) {

        }
    }

    private static void trustMain() {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509", "SC");
            InputStream in = new FileInputStream(new File("C:\\Projects\\RKK\\testdata\\appserver1\\cert.cer"));
            X509Certificate certificate = (X509Certificate) cf.generateCertificate(in);
            in.close();
            trust.add(new TrustAnchor(certificate, null));
        } catch (Exception e) {

        }
    }

	private static void testSign() throws Exception {
		AppConfig.getInstance().defaultClientRuTokenInit();
		CryptoServiceConfig config = getCryptoServiceConfig();
		
		Test.testSign(config);
		
		String test = "Test text";
	}
	
	private static CryptoServiceConfig getCryptoServiceConfig() {
		CryptoServiceConfig cconfig = null;

/*		if (this.keyStorePKCS12Button.isSelected())
		{
			cconfig = AppConfig.getInstance().getSCPKCS12CryptoServiceConfig();
		}
		else if (this.keyStoreRutokenButton.isSelected())
		{
*/			
		cconfig = AppConfig.getInstance().getRutokenCryptoServiceConfig();
/*		}
		else
		{
			cconfig = AppConfig.getInstance().getEtokenCryptoServiceConfig();
		}
*/		return cconfig;
	}
}
